﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NHI.Sync.Tools
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Current = this;
            this.DataContext = this;
        }


        public static MainWindow Current { get; private set; }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        

        public void ShowError(string msg)
        {
            try
            {

                this.Dispatcher.Invoke(() =>
                {
                    lblStatus.SetValue(Label.StyleProperty, Application.Current.Resources["text bg-danger"]);
                    lblStatus.Content = msg;
                });
            }
            catch (TaskCanceledException)
            {

            }
            new Thread(Tick).Start(msg);
        }

        public void ShowSuccess(string msg)
        {
            try
            {
                this.Dispatcher.Invoke(() =>
                {
                    lblStatus.SetValue(Label.StyleProperty, Application.Current.Resources["text bg-success"]);
                    lblStatus.Content = msg;
                });

            }
            catch (TaskCanceledException)
            {

            }
            new Thread(Tick).Start(msg);
        }

        public void ShowWarning(string msg)
        {
            try
            {
                this.Dispatcher.Invoke(() =>
            {
                lblStatus.SetValue(Label.StyleProperty, Application.Current.Resources["text bg-warning"]);
                lblStatus.Content = msg;
            });
            }
            catch (TaskCanceledException)
            {

            }
            new Thread(Tick).Start(msg);
        }


        void Tick(object msg)
        {
            Thread.Sleep(5000);
            try
            {
                this.Dispatcher.Invoke(() =>
                {
                    if (String.Equals(msg, lblStatus.Content))
                    {
                        lblStatus.Content = string.Empty;
                        lblStatus.SetValue(Label.StyleProperty, Application.Current.Resources["help-block"]);
                    }
                });
            }
            catch (TaskCanceledException)
            {

            }
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            //var p = e.GetPosition(e.Source as FrameworkElement);
            //ShowError($"x:{p.X},y:{p.Y}");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MainWindow.Current.frame_0.OnNext_Click(sender, e);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Forms.Form_Execute.Current.MyStatus.SetStatus(Forms.Form_Execute_StatusType.已停止);
            Application.Current.Shutdown();
        }
    }
}
